but given a node, how to check if it is or not, we should do this: isBalanced(root) = Math.abs(height(root.left) - height(root.right)) < 1 classSolution{publicbooleanisBalanced(TreeNode root){if(root ==null)returntrue;if(Math.abs(height(root.left) - height(root.right)) >1) {...
voiddfs(TreeNode*root,intnum,vector<int>&arr){ if(num==len-1&&root->val==arr[num]){ if(root->left==NULL&&root->right==NULL)flag=true; return; } if(root->val!=arr[num])return; if(root->left)dfs(root->left,num+1,arr); if(root->right)dfs(root->right,num+1,arr); return...
classSolution{ bool isTheEnd =false; int depth =0;public: bool isCompleteTree(TreeNode* root) {if(root == NULL)returntrue; depth = GetDepth(root);returnGetAns(root,1); } int GetDepth(TreeNode *root) {if(root == NULL)return0;returnmax(GetDepth(root->left), GetDepth(root->right)...
Given a binary tree, determine if it is acomplete binary tree. Definition of a complete binary tree fromWikipedia: 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 a...
delete_tree is_in_tree // returns true if given value exists in the tree get_height // returns the height in nodes (single node's height is 1) get_min // returns the minimum value stored in the tree get_max // returns the maximum value stored in the tree is_binary_search_tree de...
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 参考资料: ...
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)//如果一个节点没有子...
TreeNode top=null,L =null,R =null;while(!queue.isEmpty()){ top=queue.poll(); L= top.left; R =top.right;//第一种情况if((R !=null&& L ==null))returnfalse;//第二种情况 开启了判断叶子的过程 而且又不是叶子 就返回falseif(leaf && (L !=null|| R !=null))//以后的结点必须是...