}returnisBalanced(root.left) && isBalanced(root.right); }privateintheight(TreeNode root){if(root ==null)return0;returnMath.max(height(root.left), height(root.right)) +1; } } the first time i wrote this code, if(Math.abs(height(root.left) - height(root.right)) <=1) {returntrue;...
int GetDepth(TreeNode *root) {if(root == NULL)return0;returnmax(GetDepth(root->left), GetDepth(root->right)) +1; } bool GetAns(TreeNode *root, int dep) {//只有根节点的情况,不用判空,因为不会递归到那if(dep == depth) {returntrue; }if(dep < depth -1) {if(root->left == ...
https://leetcode.com/problems/check-completeness-of-a-binary-tree/discuss/205768/Java-easy-Level-Order-Traversal-one-while-loop class Solution { public: bool isCompleteTree(TreeNode* root) { bool end = false; if(root == NULL) return true; queue<TreeNode*> q; q.push(root); while(!q....
Given a binary tree, determine if it is a complete binary tree. Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1...
https://leetcode.com/problems/check-completeness-of-a-binary-tree/discuss/205768/Java-easy-Level-Order-Traversal-one-while-loop classSolution {public:boolisCompleteTree(TreeNode*root) {boolend =false;if(root ==NULL)returntrue; queue<TreeNode*>q; ...
TreeNode *cur = q.front(); q.pop();if(!cur) { found =true; }else{if(found)returnfalse; q.push(cur->left); q.push(cur->right); } }returntrue; } }; Github 同步地址: https://github.com/grandyang/leetcode/issues/958 参考资料: ...
If the tree is complete, when first met null in the queue, then the rest should all be null. Otherwise, it is not complete. Time Complexity: O(n). Space: O(n). AC Java: 1/**2* Definition for a binary tree node.3* public class TreeNode {4* int val;5* TreeNode left;6* Tr...
the definition of complete binary tree is: all the leaf nodes should be as left as it can be. and every other level is full except the bottom node level it remind me of the recursion way. but after i write the following codes, it’s not working ...
1classSolution2{3public:4boolisCompleteTree(TreeNode*root)5{6if(root ==NULL)7returnfalse;89queue<TreeNode *>q;10q.push(root);11boolmustHaveNoChild =false;12boolresult =true;13while(!q.empty())14{15TreeNode* pNode =q.front();16q.pop();17if(mustHaveNoChild)//如果一个节点没有子...
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public boolean isCompleteTree(TreeNode root) { if(root == null) return true; Queue<TreeNode>queue = ...