Description
A full binary tree is defined as a binary tree in which all nodes have either zero or two child nodes. Conversely, there is no node in a full binary tree, which has one child node. More information about full binary trees can be found here.
Full Binary Tree
1 / 2 3 / 4 5
Not a Full Binary Tree
1 / 2 3 / 4
Have you met this question in a real interview?
ExampleGiven tree {1,2,3}, return true
Given tree {1,2,3,4}, return false
Given tree {1,2,3,4,5} return true
public class Solution { /** * @param root: the given tree * @return: Whether it is a full tree */ public boolean isFullTree(TreeNode root) { // write your code here if (root == null) return true; if (root.left == null && root.right == null) return true; if (root.left == null && root.right != null) return false; if (root.left != null && root.right == null) return false; return isFullTree(root.left) && isFullTree(root.right); } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/69436.html
problem Check if two binary trees are identical. Identical means the two binary trees have the same structure and every identical position has the same value. Example 1 1 / ...
摘要:这里要注意的是的用法。所以记住,用可以从自动分离出数组。跳过第一个元素并放入数组最快捷语句建立的用意记录处理过的结点并按处理所有结点和自己的连接下面先通过判断,再修改的符号的顺序,十分巧妙更轻便的解法 Problem Design an algorithm and write code to serialize and deserialize a binary tree. Writin...
摘要:根据二叉平衡树的定义,我们先写一个求二叉树最大深度的函数。在主函数中,利用比较左右子树的差值来判断当前结点的平衡性,如果不满足则返回。 Problem Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as...
摘要:建立两个树结点,先用找到在的位置,让作为的根节点找到的位置后,指向。此时,用代替与连接就可以了。 Problem Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tr...
Problem Given a root of Binary Search Tree with unique value for each node. Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You sho...
阅读 1990·2021-09-22 16:05
阅读 9252·2021-09-22 15:03
阅读 2879·2019-08-30 15:53
阅读 1697·2019-08-29 11:15
阅读 902·2019-08-26 13:52
阅读 2347·2019-08-26 11:32
阅读 1797·2019-08-26 10:38
阅读 2561·2019-08-23 17:19