leetcode151: symmetric tree
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 3
Note:
Bonus points if you could solve it both recursively and iteratively.
提示:是否可以同時用遞迴和迭代的方法解決?
方法一:遞迴
public class SymmetricTree {
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
public boolean isSymmetric(TreeNode root) {
if(root == null) return true;
return isSymmetric(root.left, root.right);
}
public boolean isSymmetric(TreeNode leftNode, TreeNode rightNode){
if(leftNode == null && rightNode == null ) return true;
if(leftNode == null || rightNode == null) return false;
if(leftNode.val != rightNode.val) return false;
boolean isLeft = isSymmetric(leftNode.left, rightNode.right);
boolean isRight = isSymmetric(leftNode.right, rightNode.left);
return isLeft && isRight;
}
public static void main(String args[]){
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(2);
root.left.left = new TreeNode(3);
root.left.right = new TreeNode(4);
root.right.left = new TreeNode(4);
root.right.right = new TreeNode(3);
System.out.println(new SymmetricTree().isSymmetric(root));
System.out.println(new SymmetricTree().isSymmetricIter(root));
}
}
方法二:迭代
public boolean isSymmetricIter(TreeNode root) {
if(root == null) return true;
Stack<TreeNode> leftStack = new Stack<TreeNode>();
Stack<TreeNode> rightStack = new Stack<TreeNode>();
leftStack.push(root.left);
rightStack.push(root.right);
while (leftStack.size() > 0 && rightStack.size() > 0){
TreeNode left = leftStack.pop();
TreeNode right = rightStack.pop();
if(left == null && right == null) continue;
if(left == null || right == null) return false;
if(left.val == right.val) {
leftStack.push(left.right);
leftStack.push(left.left);
rightStack.push(right.left);
rightStack.push(right.right);
}else{
return false;
}
}
return true;
}
相關文章
- Leetcode Symmetric TreeLeetCode
- Leetcode-Symmetric TreeLeetCode
- Symmetric Tree leetcode javaLeetCodeJava
- 【LeetCode從零單排】No100 Same Tree && No101 Symmetric TreeLeetCode
- 【LeetCode】Symmetric Tree 推斷一棵樹是否是映象的LeetCode
- leetcode-101-Symmetric Tree-二叉樹對稱問題LeetCode二叉樹
- C. Nezzar and Symmetric Array
- 【LeetCode-面試演算法經典-Java實現】【101-Symmetric Tree(對稱樹)】LeetCode面試演算法Java
- LeetCode151:Reverse Words in a StringLeetCode
- Tree
- Rebuild TreeRebuild
- 01 Tree
- DSU on Tree
- 【MySQL(1)| B-tree和B+tree】MySql
- 多路查詢樹:B-tree/b+tree
- layui Tree修改UI
- Tree Compass
- segment tree beats
- B+tree
- B-tree
- Codeforces 461B Appleman and Tree:Tree dpAPP
- Causal Inference理論學習篇-Tree Based-Causal Tree
- Trie tree實踐
- fuelux.tree用法UX
- Utopian Tree in JavaJava
- Circular Spanning Tree
- tree的tip:WHEN-TREE-NODE-SELECTED兩次啟用
- 論文解讀GALA《Symmetric Graph Convolutional Autoencoder for Unsupervised Graph Representation Learning》
- odoo form view套treeOdooORMView
- 資料結構-Tree資料結構
- Mac OS install treeMac
- Leetcode Same TreeLeetCode
- B-tree and Bitmap IndexIndex
- DHTML Tree 使用例項HTML
- A Simple Sample for Expression Tree (轉)Express
- el-tree-select
- F - Perfect Matching on a Tree
- 高效能Mysql 入門到放棄 之 B+-Tree (與B-Tree以及Binary Tree的對比解析)MySql