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); } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/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...
閱讀 1684·2021-11-23 09:51
閱讀 3174·2021-09-26 10:21
閱讀 798·2021-09-09 09:32
閱讀 881·2019-08-29 16:06
閱讀 3308·2019-08-26 13:36
閱讀 772·2019-08-26 10:56
閱讀 2564·2019-08-26 10:44
閱讀 1143·2019-08-23 14:04