LintCode: Identical Binary Tree

C++

 1 /**
 2  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  */
13 class Solution {
14 public:
15     /**
16      * @aaram a, b, the root of binary trees.
17      * @return true if they are identical, or false.
18      */
19     bool isIdentical(TreeNode* a, TreeNode* b) {
20         // Write your code here
21         if (a == NULL && b == NULL) {
22             return true;
23         }
24         if (a == NULL || b == NULL) {
25             return false;
26         }
27         if (a->val != b->val) {
28             return false;
29         }
30         return isIdentical(a->left, b->left) && isIdentical(a->right, b->right);
31     }
32 };
时间: 2024-08-04 03:19:28

LintCode: Identical Binary Tree的相关文章

[LintCode] Identical Binary Tree 相同二叉树

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. Have you met this question in a real interview? Example 1 1 / \ / 2 2 and 2 2 / / 4 4 are identical

[LintCode] Flatten Binary Tree to Linked List 将二叉树展开成链表

Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the right pointer in TreeNode as the next pointer in ListNode. Notice Don't forget to mark the left child of each node to null. Or you will get Time Limit Exceeded

[lintcode easy]Binary Tree Paths

Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Example Given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: [ "1->2->5", "1->3" ] /** * Definition of TreeNode: * public class TreeNode {

[lintcode easy]Binary Tree Postorder Traversal

Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Challenge Can you do it without recursion? Among preorder,inorder and postorder binary tree

[Lintcode] Balanced Binary Tree

Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Example Given

[LintCode] Invert Binary Tree 翻转二叉树

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. Example Given 4 points: (1,2), (3,6), (0,0), (1,3). The maximum number is 3. LeeCode上的原题,可参见我之前的博客Invert Binary Tree 翻转二叉树. 解法一: // Recursion class So

Lintcode 71 Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). Example Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zigzag l

Identical Binary Tree

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 / \ / 2 2 and 2 2 / / 4 4 are identical. 1 1 / \ / 2 3 and 2 3 / 4 4 are not identical.

[Lintcode]7. Serialize and Deserialize Binary Tree/[Leetcode]297. Serialize and Deserialize Binary Tree

7. Serialize and Deserialize Binary Tree/297. Serialize and Deserialize Binary Tree 本题难度: Medium/Hard Topic: Binary Tree Description Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called '