Symmetric Tree leetcode java

题目:

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree is symmetric:

    1
   /   2   2
 / \ / 3  4 4  3

But the following is not:

    1
   /   2   2
   \      3    3

Note:
Bonus points if you could solve it both recursively and iteratively.

题解

判断左右子树是否相等。

递归的解法:

1  public boolean isSymmetricTree(TreeNode p,TreeNode q){
 2      if(p == null&&q == null)
 3         return true;
 4      if(p == null||q == null)
 5         return false;
 6      return (p.val == q.val) && isSymmetricTree(p.left, q.right) && isSymmetricTree(p.right, q.left);
 7 }
 8 
 9 public boolean isSymmetric(TreeNode root) {
10     if(root==null) 
11         return true;
12         
13     return isSymmetricTree(root.left,root.right);
14 }

非递归解法:

1 public boolean isSymmetric(TreeNode root) {
 2     if(root == null)
 3         return true;
 4     if(root.left == null && root.right == null)
 5         return true;
 6     if(root.left == null || root.right == null)
 7         return false;
 8     LinkedList<TreeNode> q1 = new LinkedList<TreeNode>();
 9     LinkedList<TreeNode> q2 = new LinkedList<TreeNode>();
10     q1.add(root.left);
11     q2.add(root.right);
12     while(!q1.isEmpty() && !q2.isEmpty()){
13         TreeNode n1 = q1.poll();
14         TreeNode n2 = q2.poll();
15         
16         if(n1.val != n2.val)
17             return false;
18         if((n1.left == null && n2.right != null) || (n1.left != null && n2.right == null))
19             return false;
20         if((n1.right == null && n2.left != null) || (n1.right != null && n2.left == null))
21             return false;
22         
23         if(n1.left != null && n2.right != null){
24             q1.add(n1.left);
25             q2.add(n2.right);
26         }
27         
28         if(n1.right != null && n2.left != null){
29             q1.add(n1.right);
30             q2.add(n2.left);
31         }            
32     }
33     return true;
34 }

Reference:

http://blog.csdn.net/linhuanmars/article/details/23072829

Symmetric Tree leetcode java,布布扣,bubuko.com

时间: 2024-12-27 10:42:07

Symmetric Tree leetcode java的相关文章

Minimum Depth of Binary Tree leetcode java

题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 题解: 递归解法急速判断左右两边子树哪个depth最小,要注意如果有个节点只有一边孩子时,不能返回0,要返回另外一半边的depth. 递归解法: 1     public 

Same Tree leetcode java

题目: 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. 题解: 递归判断左右是否相等. 代码如下: 1     public boolean isSameTree(TreeNode

Maximum Depth of Binary Tree leetcode java

题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解: 递归解法: 1     public int maxDepth(TreeNode root) {2         if(root==null)3         

Convert Sorted List to Binary Search Tree leetcode java

题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题解: 之前做过一道是从sorted array转换到BinarySearchTree的,方法还是一样二分法.但是构造树的方法不是由顶至下了,是要由低至上的建立. 代码如下: 1     static ListNode h; 2   3     public TreeNo

Convert Sorted Array to Binary Search Tree leetcode java

题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题解: 先复习下什么是二叉搜索树(引自Wikipedia): 二叉查找树(Binary Search Tree),也称有序二叉树(ordered binary tree),排序二叉树(sorted binary tree),是指一棵空树或者具有下列性质的二叉树: 若任意节点的左子树不空,则左子树

Balanced Binary Tree leetcode java

题目: 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. 题解: 采用递归的方法,要记录depth用来比较. 代码如下:

Recover Binary Search Tree leetcode java

题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? 题解: 解决方法是利用中序遍历找顺序不对的两个点

Validate Binary Search Tree leetcode java

题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with

Symmetric Tree leetcode

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 看到这个题首先想到的是中序遍历,遍历结果是否是前后相同,也就是是否是“回文”,我使用了一个栈,遍历