【LeetCode】Symmetric Tree 判斷一棵樹是否是映象的
<span style="font-size:18px;"><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></span>
相關推薦
【LeetCode】Symmetric Tree 判斷一棵樹是否是映象的
<span style="font-size:18px;"><span style="font-size:18px;">/**LeetCode Symmetric Tree 對稱的樹 * 思路:判斷一棵樹是否對稱,1.有左子樹就要有右子樹 *
【LeetCode】Symmetric Tree(對稱二叉樹)
這道題是LeetCode裡的第101道題。是我在學資料結構——二叉樹的時候碰見的題。 題目如下: 給定一個二叉樹,檢查它是否是映象對稱的。 例如,二叉樹 [1,2,2,3,4,4,3] 是對稱的。 1 / \ 2 2 / \ / \ 3
leetcode101. Symmetric Tree判斷一棵樹是否是映象二叉樹
思路:轉化為求兩個二叉樹是否是映象二叉樹class Solution { //判斷一顆二叉樹是否是映象的 public boolean isSymmetric(TreeNode root) { if(root==null){
【LeetCode筆記】判斷一棵樹是否為映象
最初思路: 1.求樹的中序遍歷,存入vector; 2.判斷vector是否為為映象; /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo
LeetCode:572. Subtree of Another Tree(判斷一棵樹是不是另外一棵樹的子樹)
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree
【資料結構】判斷一棵樹是否為完全二叉樹
完全二叉樹(Complete Binary Tree) 若設二叉樹的深度為h,除第h層外,其它各層(1~h-1)的結點數都達到最大個數,第h層所有的結點都連續集中在最左邊,這就是完全二叉樹。 完全二叉樹是由滿二叉樹而引出來的。對於深度為K的,有n個結點的二叉樹,當且僅當其每
leetcode----- Validate Binary Search Tree(判斷一棵樹是否是二叉搜尋樹)
1、題目描述 給定一棵二叉樹,判斷這棵樹是否是二叉搜尋樹。 二叉搜尋樹的定義如下: 二叉搜尋樹(Binary Search Tree),又稱二叉排序樹,它或者是一顆空樹,或者具有如下性質的樹: 若它的左子樹不為空,則左子樹上所有節點的值都小於根節點的值 若它的右子樹
【leetcode】136.只出現一次的數字 c++實現
【leetcode】136.只出現一次的數字 c++實現 題目 給定一個非空整數陣列,除了某個元素只出現一次以外,其餘每個元素均出現兩次。找出那個只出現了一次的元素。 例1: 輸入: [2,2,1] 輸出: 1 例2 輸入: [4,1,2,1,2] 輸出: 4
【LeetCode】137. 只出現一次的數字 II 結題報告 (C++)
原題地址:https://leetcode-cn.com/problems/single-number-ii/description/ 題目描述: 給定一個非空整數陣列,除了某個元素只出現一次以外,其餘每個元素均出現了三次。找出那個只出現了一次的元素。 說明: 你的演算法應該具有線性
Is It A Red-Black Tree?(判斷一棵樹是否為紅黑二叉樹)
以前沒接觸過這種樹,看了別人的程式碼,加上了詳細的註釋。 思路全在註釋中。 我將測試樣例放上,以便複製。 3 9 7 -2 1 5 -4 -11 8 14 -15 9 11 -2 1 -7 5 -4 8 14 -15 8 10 -7 5 -6 8 15 -11 17
【LeetCode】Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2
【leetcode】中文版題庫一刷ing
目錄 LINK 1. 兩數之和 給定一個整數陣列和一個目標值,找出陣列中和為目標值的兩個數。 你可以假設每個輸入只對應一種答案,且同樣的元素不能被重複利用。 示例: 給定 nums = [2, 7, 11, 15], target = 9 因
【LeetCode】#137只出現一次的數字II(Single Number II)
【LeetCode】#137只出現一次的數字II(Single Number II) 題目描述 給定一個非空整數陣列,除了某個元素只出現一次以外,其餘每個元素均出現了三次。找出那個只出現了一次的元素。 說明: 你的演算法應該具有線性時間複雜度。 你可以不使用額外空間來實現嗎? 示
【LeetCode】#136只出現一次的數字(Single Number)
【LeetCode】#136只出現一次的數字(Single Number) 題目描述 給定一個非空整數陣列,除了某個元素只出現一次以外,其餘每個元素均出現兩次。找出那個只出現了一次的元素。 說明: 你的演算法應該具有線性時間複雜度。 你可以不使用額外空間來實現嗎? 示例 示例
【leetcode】same-tree
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 identic
leetcode 101. 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 symmetr
【LeetCode】Binary Tree Inorder Traversal 二叉樹中序遍歷遞迴以及非遞迴演算法
Total Accepted: 16494 Total Submissions: 47494 Given a binary tree, return the inorder traversal of its nodes' values. For example: Giv
【leetCode】Binary Tree Level Order Traversal python實現
Binary Tree Level Order Traversal 原題連結 實現原理解析 層次遍歷即可 python程式碼實現 class Solution(object): def __init__(self
【LeetCode】Binary Tree Preorder Traversal 二叉樹前序遍歷遞迴以及非遞迴演算法
Total Accepted: 17403 Total Submissions: 50093 My Submissions Given a binary tree, return the preorder traversal of its nodes' values.
【Leetcode】136. 只出現一次的數字
題目描述:給定一個非空整數陣列,除了某個元素只出現一次以外,其餘每個元素均出現兩次。找出那個只出現了一次的元素。說明:你的演算法應該具有線性時間複雜度。 你可以不使用額外空間來實現嗎?示例 1:輸入: [2,2,1] 輸出: 1 示例 2:輸入: [4,1,2,1,2] 輸出