Binary Tree Level Order Traversal 参考资料: https://leetcode.com/problems/cousins-in-binary-tree/ https://leetcode.com/problems/cousins-in-binary-tree/discuss/238536/My-Easy-Recursive-C%2B%2B-Solution https://leetcode.com/problems/cousins-in-binary-tree/discuss/239376/Java-BFS-time-and-space...
*/publicbooleanisCousins(TreeNode root,intx,inty){int[] arr = getTreeDepth(root, x);int[] arr2 = getTreeDepth(root, y);if(arr.length <1|| arr2.length <1) {returnfalse; }returnarr[0] != arr2[0] && arr[1] == arr2[1]; }publicint[] getTreeDepth(TreeNode root,intnum) ...
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right (right) {} * }; */ classSolution{ public: TreeNode*replaceValueInTree(TreeNode*root) { ...
0094-Binary-Tree-Inorder-Traversal 0095-Unique-Binary-Search-Trees-II 0096-Unique-Binary-Search-Trees 0098-Validate-Binary-Search-Tree 0099-Recover-Binary-Search-Tree 0100-Same-Tree 0101-Symmetric-Tree 0102-Binary-Tree-Level-Order-Traversal 0103-Binary-Tree-Zigzag-Level-Orde...
0094-Binary-Tree-Inorder-Traversal 0095-Unique-Binary-Search-Trees-II 0096-Unique-Binary-Search-Trees 0098-Validate-Binary-Search-Tree 0099-Recover-Binary-Search-Tree 0100-Same-Tree 0101-Symmetric-Tree 0102-Binary-Tree-Level-Order-Traversal 0103-Binary-Tree-Zigzag-Level-Ord...
In a binary tree, the root node is at depth0, and children of each depthknode are at depthk+1. Two nodes of a binary tree arecousinsif they have the same depth, but have different parents. We are given therootof a binary tree with unique values, and the valuesxandyof two different...
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */classSolution{public:boolgetPath(TreeNode* root,intx, vector<int>& path){if(root ==NULL){return...
1 class Solution 2 { 3 public: 4 bool judge(TreeNode* root, int x, int y) 5 { 6 if(root!=NULL) 7 { 8 9 if(root->left&&root->right...
Given the root of a binary tree with unique values and the values of two different nodes of the tree x and y, return true if the nodes corresponding to the values x and y in the tree are cousins, or false otherwise. Two nodes of a binary tree are cousins if they have the same dep...
In a binary tree, the root node is at depth0, and children of each depthknode are at depthk+1. Two nodes of a binary tree arecousinsif they have the same depth, but have different parents. We are given therootof a binary tree with unique values, and the valuesxandyof two different...