LeetCode: isSameTree1 解题报告

isSameTree1

Given two binary trees, write a function to check if they are equal or not.

Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

SOLUTION 1 & SOLUTION 2:

以下是递归及非递归解法:

1. 递归解法就是判断当前节点是不是相同,及左右子树是不是相同树

2. 非递归解法使用了先根遍历。这个算法比较简单一点

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     // solution 1:
12     public boolean isSameTree1(TreeNode p, TreeNode q) {
13         if (p == null && q == null) {
14             return true;
15         }
16
17         if (p == null || q == null) {
18             return false;
19         }
20
21         return p.val == q.val &&
22            isSameTree(p.left, q.left) &&
23            isSameTree(p.right, q.right);
24     }
25
26     // Solution 2:
27     public boolean isSameTree(TreeNode p, TreeNode q) {
28         if (p == null && q == null) {
29             return true;
30         }
31
32         if (p == null || q == null) {
33             return false;
34         }
35
36         Stack<TreeNode> s1 = new Stack<TreeNode>();
37         Stack<TreeNode> s2 = new Stack<TreeNode>();
38
39         s1.push(p);
40         s2.push(q);
41
42         while (!s1.isEmpty() && !s2.isEmpty()) {
43             TreeNode cur1 = s1.pop();
44             TreeNode cur2 = s2.pop();
45
46             // 弹出的节点的值必须相等
47             if (cur1.val != cur2.val) {
48                 return false;
49             }
50
51             // tree1的right节点,tree2的right节点,可以同时不为空,也可以同时为空,否则返回false.
52             if (cur1.left != null && cur2.left != null) {
53                 s1.push(cur1.left);
54                 s2.push(cur2.left);
55             } else if (!(cur1.left == null && cur2.left == null)) {
56                 return false;
57             }
58
59             // tree1的左节点,tree2的left节点,可以同时不为空,也可以同时为空,否则返回false.
60             if (cur1.right != null && cur2.right != null) {
61                 s1.push(cur1.right);
62                 s2.push(cur2.right);
63             } else if (!(cur1.right == null && cur2.right == null)) {
64                 return false;
65             }
66         }
67
68         return true;
69     }
70 }

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/IsSameTree1.java

时间: 2024-11-23 11:23:23

LeetCode: isSameTree1 解题报告的相关文章

LeetCode: Permutations 解题报告

Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. SOLUTION 1: 经典的递归回溯题目,一次ACCEPT. 请也参考上一个题目LeetCode: Combination

[LeetCode]3Sum,解题报告

题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The so

[LeetCode]Candy, 解题报告

前言 回学校写论文差不多快二周的时间了,总结一句话,在学校真舒服.花了7天时间搞定了毕业论文初稿(之所以这么快肯定跟我这三年的工作量相关了),不过今天导师批复第二章还是需要修改.此外,最近迷上打羽毛球,确实很爽,运动量仅此于篮球,可以场地有限,哎,且打且珍惜吧. 题目 There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these chi

Single Number | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/single-number/ 题目描述: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without usin

Add Two Numbers | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/add-two-numbers/ 题目描述: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i

Two Sum | LeetCode OJ 解题报告

题目网址:https://oj.leetcode.com/problems/two-sum/ 题目描述: Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, whe

LeetCode: Subsets 解题报告

Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1

LeetCode: solveSudoku 解题报告

Sudoku SolverWrite a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. SOLUTION: ref: http://blog.csdn.net/fightforyourdream/articl

LeetCode: Triangle 解题报告

Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle[     [2],    [3,4],   [6,5,7],  [4,1,8,3]]The minimum path sum from top to