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.
Example1 1 / / 2 2 and 2 2 / / 4 4 are identical. 1 1 / / 2 3 and 2 3 / 4 4 are not identical.Solution
public class solution { public boolean isIdentical(TreeNode a, TreeNode b) { if (a == null && b == null) return true; if (a == null || b == null) return false; return a.val == b.val && isIdentical(a.left, b.left) && isIdentical(a.right, b.right); } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/65411.html
摘要:原题检查两棵二叉树是否在经过若干次扭转后可以等价。扭转的定义是,交换任意节点的左右子树。等价的定义是,两棵二叉树必须为相同的结构,并且对应位置上的节点的值要相等。样例是扭转后可等价的二叉树。 原题检查两棵二叉树是否在经过若干次扭转后可以等价。扭转的定义是,交换任意节点的左右子树。等价的定义是,两棵二叉树必须为相同的结构,并且对应位置上的节点的值要相等。注意:你可以假设二叉树中不会有重复...
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 in...
摘要:根据二叉平衡树的定义,我们先写一个求二叉树最大深度的函数。在主函数中,利用比较左右子树的差值来判断当前结点的平衡性,如果不满足则返回。 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...
阅读 1662·2021-11-23 09:51
阅读 3117·2021-09-26 10:21
阅读 772·2021-09-09 09:32
阅读 848·2019-08-29 16:06
阅读 3278·2019-08-26 13:36
阅读 759·2019-08-26 10:56
阅读 2549·2019-08-26 10:44
阅读 1130·2019-08-23 14:04