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.
分析:二叉树的问题,可以选择递归算法,也可以选择遍历二叉树,对应结点相等的方式来判断。
解法一:非递归算法,需要考虑多种情况。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { LinkedList<TreeNode> stack1 = new LinkedList<TreeNode>(); LinkedList<TreeNode> stack2 = new LinkedList<TreeNode>(); if(p == null && q == null){ return true; }else if(p != null && q != null){ stack1.add(p); stack2.add(q); while(stack1.size() != 0 && stack2.size() != 0){ TreeNode node1 = stack1.removeFirst(); TreeNode node2 = stack2.removeFirst(); if(node1 == null && node2 == null){ continue; }else if(node1 != null && node2 != null){ if(node1.val != node2.val){ return false; } stack1.add(node1.left); stack2.add(node2.left); stack1.add(node1.right); stack2.add(node2.right); }else{ return false; } } return true; }else{ return false; } } }
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { LinkedList<TreeNode> stack1 = new LinkedList<TreeNode>(); LinkedList<TreeNode> stack2 = new LinkedList<TreeNode>(); if((p == null && q == null) || (p != null && q != null)){ stack1.add(p); stack2.add(q); while(stack1.size() != 0 && stack2.size() != 0){ TreeNode node1 = stack1.removeFirst(); TreeNode node2 = stack2.removeFirst(); if(node1 == null && node2 == null){ continue; }else if(node1 != null && node2 != null){ if(node1.val != node2.val){ return false; } stack1.add(node1.left); stack2.add(node2.left); stack1.add(node1.right); stack2.add(node2.right); }else{ return false; } } return true; }else{ return false; } } }
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { // 非递归算法 // LinkedList<TreeNode> stack1 = new LinkedList<TreeNode>(); // LinkedList<TreeNode> stack2 = new LinkedList<TreeNode>(); // if((p == null && q == null) || (p != null && q != null)){ // stack1.add(p); // stack2.add(q); // while(stack1.size() != 0 && stack2.size() != 0){ // TreeNode node1 = stack1.removeFirst(); // TreeNode node2 = stack2.removeFirst(); // if(node1 == null && node2 == null){ // continue; // }else if(node1 != null && node2 != null){ // if(node1.val != node2.val){ // return false; // } // stack1.add(node1.left); // stack2.add(node2.left); // stack1.add(node1.right); // stack2.add(node2.right); // }else{ // return false; // } // } // return true; // }else{ // return false; // } //递归算法 if(p == null && q == null) return true; else if(p == null || q == null) return false; else{ if(p.val != q.val) return false; } boolean left = isSameTree(p.left, q.left); boolean right = isSameTree(p.right, q.right); return left && right; } }