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  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.

Iterative function:

 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     public boolean isSymmetric(TreeNode root) {

12         // Start typing your Java solution below

13         // DO NOT write main() function

14         LinkedList<TreeNode> left = new LinkedList<TreeNode>();

15         LinkedList<TreeNode> right = new LinkedList<TreeNode>();

16         if(root == null) return true;

17         else if(root.left == null && root.right == null) return true;

18         else if((root.left == null && root.right != null) 
         || (root.left != null && root.right == null)) return false; 19 else{ 20 left.add(root.left); 21 right.add(root.right); 22 while(!left.isEmpty() && !right.isEmpty()){ 23 TreeNode l = left.pop(); 24 TreeNode r = right.pop(); 25 if(l.val != r.val) return false; 26 if(l.left != null && r.right != null){ 27 left.add(l.left); 28 right.add(r.right); 29 }else if((l.left != null && r.right == null)
               || (l.left == null && r.right != null)){ 30 return false; 31 } 32 if(l.right != null && r.left != null){ 33 left.add(l.right); 34 right.add(r.left); 35 }else if((l.right != null && r.left == null)
               || (l.right == null && r.left != null)){ 36 return false; 37 } 38 } 39 } 40 return true; 41 } 42 43 public void checkNode(){ 44 45 } 46 }

 

Recursive function:

 

 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     boolean rsl = true;

12     public boolean isSymmetric(TreeNode root) {

13         // Start typing your Java solution below

14         // DO NOT write main() function

15         rsl = true;

16         if(root == null) return rsl;

17         else{

18             checkNode(root.left,root.right);

19         }

20         return rsl;

21     }

22     

23     public void checkNode(TreeNode l, TreeNode r){

24         if(l == null && r == null ){

25             return;

26         }else if((l == null && r != null) || (l != null && r == null)){

27             rsl = false;

28             return;

29         }else{

30             checkNode(l.left, r.right);

31             checkNode(l.right,r.left);

32             if(l.val != r.val)rsl =false;

33             return;

34         }

35     }

36 }

 第二遍:

 1 public class Solution {

 2     public boolean isSymmetric(TreeNode root) {

 3         // IMPORTANT: Please reset any member data you declared, as

 4         // the same Solution instance will be reused for each test case.

 5         return root == null || compare(root.left, root.right);

 6     }

 7     public boolean compare(TreeNode t1, TreeNode t2){

 8         if(t1 == null && t2 == null) return true;

 9         else if((t1 == null || t2 == null) || (t1.val != t2.val))return false;

10         else return compare(t1.left, t2.right) && compare(t1.right, t2.left);

11     }

12 }

 

你可能感兴趣的:(tree)