【LeetCode】Symmetric Tree 判断一棵树是否是镜像的

题目:Symmetric Tree

<span style="font-size:18px;">/**LeetCode Symmetric Tree 对称的树
 * 思路:判断一棵树是否对称,1.有左子树就要有右子树
 * 						2.除根节点外对称节点值要相同
 * 注意:对称后就是左子树的左节点和右子树的右节点比较
	 * Definition for binary tree
	 * public class TreeNode {
	 *     int val;
	 *     TreeNode left;
	 *     TreeNode right;
	 *     TreeNode(int x) { val = x; }
	 * }
	 */
package javaTrain;

public class Train8 {
	    public boolean isSymmetric(TreeNode root) {
	    	if(root == null) return true;
	    	if(root.left == null && root.right == null) return true;
	    	else if(root.left == null || root.right == null) return false;
	    	return help(root.left,root.right);
	    }
	    private boolean help(TreeNode left,TreeNode right){
	    	if(left == null && right == null) return true;
	    	else if(left == null || right == null) return false;
	    	if(left.val == right.val)
	    		return help(left.left,right.right ) && help(left.right,right.left);
	    	else return false;
	    }
}
</span>
时间: 2024-10-11 07:49:14

【LeetCode】Symmetric Tree 判断一棵树是否是镜像的的相关文章

Leetcode:Symmetric Tree 判断对称树

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 解题分析: 二叉树递归,始终是第一颗二叉树的左子树和第二颗二叉树的右

101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树

给定一个二叉树,检查它是否是它自己的镜像(即,围绕它的中心对称).例如,这个二叉树 [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说明:如果你可以递归地和迭代地解决它就奖励你点数.详见:https://leetcode.com/problems/symmetric-tree/description

LeetCode:Same Tree - 判断两颗树是否相等

1.题目名称 Same Tree(判断两棵树是否相等) 2.题目地址 https://leetcode.com/problems/same-tree/ 3.题目内容 英文: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 h

LeetCode: Symmetric Tree [101]

[题目] 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 bot

LeetCode: Symmetric Tree 解题报告

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  3But the following is not:    1   / \  2   2   \   \   3    3Note:

【LeetCode-面试算法经典-Java实现】【100-Same Tree(两棵树是否相同)】

[100-Same Tree(两棵树是否相同)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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. 题目大

判断一棵树是否是另一棵树的子树

问题 判断一棵树是否是另一棵树的子树,如图 思路 问题分两步: 找值相同的根结点(遍历解决) 判断两结点是否包含(递归:值.左孩子.右孩子分别相同) 代码 bool IsPart(TreeNode *root1, TreeNode *root2) { if (root2 == NULL) return true; if (root1 == NULL) return false; if (root1->val != root2->val) return false; return IsPart(

判断一棵树是否为完全二叉树

完全二叉树:若一棵二叉树具有具有n个节点,它的每个节点都与高度为k的满二叉树编号为0~n-1结点一一对应,则称这可二叉树为完全二叉树. 方法一:一维数组存储 根据完全二叉树的定义和性质,利用一位数组作为完全二叉树的存储,如下图 由图,节点的编号与数组元素的下标是一一对应的,可根据二叉树的性质,可方便找出下标 为i的的双亲结点a[i/2]及左右孩子结点a[i*2],a[i*2+1].这样判断一棵树是否为二叉树,应该对此二叉树从上到下,从左到右依次编号, 然后把编好的号依次存入一位数组中,在与相应深

判断一棵树是否是二叉搜索树

前两天写过一篇博文<二叉搜索树基本操作实现>,为了更深入了解二叉搜索树的性质,本文实现判断一棵树是否为二叉搜索树算法. 二叉搜索树的性质: 任意节点的键值一定大于其左子树中的每一个节点的键值,并小于其右子树中的每一个节点的键值. 构造二叉树的节点定义为: struct TreeNode{ int data; TreeNode *left; TreeNode *right; }; 方法1 (错误) 对每一个节点,检测其值是否大于左子树节点,是否小于右子树节点.思路很简单,代码实现如下: bool