zoukankan      html  css  js  c++  java
  • LeetCode101----对称二叉树

    给定一个二叉树,检查它是否是镜像对称的。

    例如,二叉树 [1,2,2,3,4,4,3] 是对称的。

        1
       / 
      2   2
     /  / 
    3  4 4  3
    

    但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:

        1
       / 
      2   2
          
       3    3

    代码如下:
    public class LeetCode101 {
    	public boolean isSymmetric = true;
    
    	public static class TreeNode {
    		int val;
    		TreeNode left;
    		TreeNode right;
    
    		TreeNode(int x) {
    			val = x;
    		}
    	}
    
    	public boolean isSymmetric(TreeNode root) {
    		if (root == null) {
    			return true;
    		}
    		isSymmetric(root.left, root.right);
    		return isSymmetric;
    	}
    
    	public void isSymmetric(TreeNode left, TreeNode right) {
    		if (left == null || right == null) {
    			if (left != right) {
    				isSymmetric = false;
    			}
    			return;
    		}
    		isSymmetric(left.left, right.right);
    		isSymmetric(left.right, right.left);
    		if (left != null && right != null) {
    			if (left.val != right.val) {
    				isSymmetric = false;
    			}
    		}
    	}
    }
    

      非递归代码如下所示:

    public class NonLeetCode101 {
    	public boolean isSymmetric = true;
    
    	public static class TreeNode {
    		int val;
    		TreeNode left;
    		TreeNode right;
    
    		TreeNode(int x) {
    			val = x;
    		}
    	}
    
    	public boolean isSymmetric(TreeNode root) {
    		if (root == null) {
    			return true;
    		}
    		isSymmetric(root.left, root.right);
    		return isSymmetric;
    	}
    
    	public void isSymmetric(TreeNode left, TreeNode right) {
    		if (left == null || right == null) {
    			if (left != right) {
    				isSymmetric = false;
    			}
    			return;
    		}
    		if (left.val != right.val) {
    			isSymmetric = false;
    			return;
    		}
    		Stack<TreeNode> lStack = lStruct(left);
    		Stack<TreeNode> rStack = rStruct(right);
    		if (lStack.size() != rStack.size()) {
    			isSymmetric = false;
    			return;
    		}
    		System.out.println("栈大小:" + lStack.size());
    		while (lStack.size() > 0) {
    			TreeNode n1 = lStack.pop();
    			TreeNode n2 = rStack.pop();
    			if (n1 == null || n2 == null) {
    				if (n1 != n2) {
    					isSymmetric = false;
    					return;
    				}
    			} else if (n1 != null && n2 != null) {
    				if (n1.val != n2.val) {
    					isSymmetric = false;
    					return;
    				}
    			}
    		}
    	}
    
    	private Stack<TreeNode> lStruct(TreeNode root) {
    		Stack<TreeNode> stack = new Stack<>();
    		Stack<TreeNode> reStack = new Stack<>();
    		stack.push(root);
    		while (!stack.isEmpty()) {
    			root = stack.pop();
    			reStack.push(root);
    			if (root != null) {
    				if (root.right != null) {
    					stack.push(root.right);
    				} else if (root.right == null) {
    					stack.push(null);
    				}
    				if (root.left != null) {
    					stack.push(root.left);
    				} else if (root.left == null) {
    					stack.push(null);
    				}
    			}
    		}
    		return reStack;
    	}
    
    	private Stack<TreeNode> rStruct(TreeNode root) {
    		Stack<TreeNode> stack = new Stack<>();
    		Stack<TreeNode> reStack = new Stack<>();
    		stack.push(root);
    		while (!stack.isEmpty()) {
    			root = stack.pop();
    			reStack.push(root);
    			if (root != null) {
    				if (root.left != null) {
    					stack.push(root.left);
    				} else if (root.left == null) {
    					stack.push(null);
    				}
    				if (root.right != null) {
    					stack.push(root.right);
    				} else if (root.right == null) {
    					stack.push(null);
    				}
    			}
    		}
    		return reStack;
    	}
    }
    

     递归代码如下:

    public class LeetCode101 {
    	public boolean isSymmetric = true;
    
    	public static class TreeNode {
    		int val;
    		TreeNode left;
    		TreeNode right;
    
    		TreeNode(int x) {
    			val = x;
    		}
    	}
    
    	public boolean isSymmetric(TreeNode root) {
    		if (root == null) {
    			return true;
    		}
    		isSymmetric(root.left, root.right);
    		return isSymmetric;
    	}
    
    	public void isSymmetric(TreeNode left, TreeNode right) {
    		if (left == null || right == null) {
    			if (left != right) {
    				isSymmetric = false;
    			}
    			return;
    		}
    		isSymmetric(left.left, right.right);
    		isSymmetric(left.right, right.left);
    		if (left != null && right != null) {
    			if (left.val != right.val) {
    				isSymmetric = false;
    			}
    		}
    	}
    }
    

      

  • 相关阅读:
    Something about the "BSTR" and "SysStringLen"
    关于 i = i ++ 的问题
    duilib写个三国杀?
    关于WM_GETTEXT的应用
    hoops暂时用过的一些方法
    Hoops随便记的
    C++ win32线程数上限
    windows系统时间(SYSTEMTIME)
    Form表单提交的那些事
    多行文字溢出...
  • 原文地址:https://www.cnblogs.com/Booker808-java/p/9058397.html
Copyright © 2011-2022 走看看