LeetCode 872. 叶子相似的树 Java/C++ 递归法&迭代法

Posted oyzg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 872. 叶子相似的树 Java/C++ 递归法&迭代法相关的知识,希望对你有一定的参考价值。

Java/C++ 递归法&迭代法

链接:https://leetcode-cn.com/problems/leaf-similar-trees/solution/javac-di-gui-fa-die-dai-fa-by-oyzg-8ngi/

解题思路
两种方法:
方法一:
dfs 前序遍历 用两个集合装叶子节点
然后判断两个集合是否相同
方法二:
迭代法 用栈模拟递归
用一个集合装root1的叶子节点
遍历root2时判断2个叶子节点序列是否相等

方法一:
Java代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode 
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() 
 *     TreeNode(int val)  this.val = val; 
 *     TreeNode(int val, TreeNode left, TreeNode right) 
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     
 * 
 */
class Solution 
    public boolean leafSimilar(TreeNode root1, TreeNode root2) 
		List<Integer> l1 = new ArrayList<Integer>();
		List<Integer> l2 = new ArrayList<Integer>();
		dfs(root1,l1);
		dfs(root2,l2);
		if(l1.size() != l2.size()) 
			return false;
		
		for(int i = 0; i < l1.size(); i++) 
			if(l1.get(i) != l2.get(i)) return false;
		
		return true;
	
	
	public void dfs(TreeNode root, List<Integer> list) 
		if(root == null) 
			return ;
		
		if(root.left == null && root.right == null) 
			list.add(root.val);
			return ;
		
		if(root.left != null) dfs(root.left,list);
		if(root.right != null) dfs(root.right, list);
	

C++代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode 
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) 
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) 
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) 
 * ;
 */
class Solution 
public:
    bool leafSimilar(TreeNode* root1, TreeNode* root2) 
        vector<int> l1;
        vector<int> l2;
		dfs(root1,l1);
		dfs(root2,l2);
		if(l1.size() != l2.size()) 
			return false;
		
		for(int i = 0; i < l1.size(); i++) 
			if(l1[i] != l2[i]) return false;
		
		return true;
    
    void dfs(TreeNode* root, vector<int>& list) 
        if(root == nullptr) 
			return ;
		
		if(root->left == nullptr && root->right == nullptr) 
			list.push_back(root->val);
			return ;
		
		if(root->left != nullptr) dfs(root->left,list);
		if(root->right != nullptr) dfs(root->right, list);
    
;

方法二:
Java代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode 
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() 
 *     TreeNode(int val)  this.val = val; 
 *     TreeNode(int val, TreeNode left, TreeNode right) 
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     
 * 
 */
class Solution 
    public boolean leafSimilar(TreeNode root1, TreeNode root2) 
        Stack<TreeNode> s1 = new Stack<TreeNode>();
		Stack<TreeNode> s2 = new Stack<TreeNode>();
		List<Integer>list = new ArrayList<Integer>();
		int index = 0;
		s1.add(root1);
		s2.add(root2);
		while(s1.isEmpty() == false ) 
			TreeNode node = s1.pop();
			if(node.left == null && node.right == null) list.add(node.val);
			if(node.right != null) s1.add(node.right);
			if(node.left != null) s1.add(node.left);
		
		while(s2.isEmpty() == false ) 
			TreeNode node = s2.pop();
			if(node.left == null && node.right == null) 
				if(index >= list.size() || node.val != list.get(index)) 
					return false;
				 else 
					index++;
				
			
			if(node.right != null) s2.add(node.right);
			if(node.left != null) s2.add(node.left);
		
		return index == list.size();
    

c++代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode 
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) 
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) 
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) 
 * ;
 */
class Solution 
public:
    bool leafSimilar(TreeNode* root1, TreeNode* root2) 
        stack<TreeNode*> s1;
		stack<TreeNode*> s2;
		vector<int> list;
		int index = 0;
		s1.push(root1);
		s2.push(root2);
		while(s1.empty() == false ) 
			TreeNode* node = s1.top();
            s1.pop();
			if(node->left == nullptr && node->right == nullptr) list.push_back(node->val);
			if(node->right != nullptr) s1.push(node->right);
			if(node->left != nullptr) s1.push(node->left);
		
		while(s2.empty() == false ) 
			TreeNode* node = s2.top();
            s2.pop();
			if(node->left == nullptr && node->right == nullptr) 
				if(index >= list.size() || node->val != list[index]) 
					return false;
				 else 
					index++;
				
			
			if(node->right != nullptr) s2.push(node->right);
			if(node->left != nullptr) s2.push(node->left);
		
		return index == list.size();
    
;

以上是关于LeetCode 872. 叶子相似的树 Java/C++ 递归法&迭代法的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 872. 叶子相似的树 Java/C++ 递归法&迭代法

leetcode 872. 叶子相似的树

leetcode872. 叶子相似的树(c++)

LeetCode 872. 叶子相似的树

LeetCode 872 叶子相似的树[DFS 二叉树] HERODING的LeetCode之路

leetcode 872. 叶子相似的树(Leaf-Similar Trees)