1. 程式人生 > >[leetcode]解決Symmetric Tree的一點小心得

[leetcode]解決Symmetric Tree的一點小心得

本次選擇的題目是

Given a binary tree, check whether it is a mirror of itself (ie,symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1
   / \
  2   2
 / \ / \
3  4 4  3

But the following [1,2,2,null,3,null,3] is not:

    1
   / \     
  2   2
   \   \
    3   3

Note: Bonus points if you could solve it both recursively and iteratively.

Solution:
運用遞迴的方法:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public
boolean isSymmetric(TreeNode root) { return root == null || isSymmetricforLandR(root.left, root.right); } private bool isSymmetricforLandR(TresNode l, TreeNode r){ if(l == null || r == null){ return l == r; } if(l.val != r.val){ return
false; } return isSymmetricforLandR(l.left, r.right) && isSymmetricforLandR(l.right, r.left); } }

不用遞迴:

public boolean isSymmetric(TreeNode root) {
    if(root==null)  return true;

    Stack<TreeNode> stack = new Stack<TreeNode>();
    TreeNode left, right;
    if(root.left!=null){
        if(root.right==null) return false;
        stack.push(root.left);
        stack.push(root.right);
    }
    else if(root.right!=null){
        return false;
    }

    while(!stack.empty()){
        if(stack.size()%2!=0)   return false;
        right = stack.pop();
        left = stack.pop();
        if(right.val!=left.val) return false;

        if(left.left!=null){
            if(right.right==null)   return false;
            stack.push(left.left);
            stack.push(right.right);
        }
        else if(right.right!=null){
            return false;
        }

        if(left.right!=null){
            if(right.left==null)   return false;
            stack.push(left.right);
            stack.push(right.left);
        }
        else if(right.left!=null){
            return false;
        }
    }

    return true;
}

相關推薦

[leetcode]解決Symmetric Tree一點心得

本次選擇的題目是 Given a binary tree, check whether it is a mirror of itself (ie,symmetric around its center). For example, this b

[leetcode]解決Kth Largest Element in an Array的一點心得

本次選擇的題目是 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not

[leetcode]解決House Robber的一點心得

本次選擇的題目是 You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stash

[leetcode]解決Move Zeroes的一點心得

本次選擇的題目是 Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the

ASP.NET MVC Autofac依賴註入的一點心得(包含特性註入)

使用 quest ont var nbsp sin posit size 繼承關系 前言 IOC的重要性 大家都清楚..便利也都知道..新的ASP.NET Core也大量使用了這種手法.. 一直憋著沒寫ASP.NET Core的文章..還是怕誤導大家.. 今天這篇也不是講C

leetcode--101. Symmetric Tree

win leet -- target 應該 efi iter int 開始 1、問題描述 Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center

[Leetcode]101. Symmetric Tree

blog enter following lean 位置 his class 兩個 oot Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its cente

[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 [1,2,2,3,4,4,3] is

LeetCodeSymmetric Tree(對稱二叉樹)

這道題是LeetCode裡的第101道題。是我在學資料結構——二叉樹的時候碰見的題。 題目如下: 給定一個二叉樹,檢查它是否是映象對稱的。 例如,二叉樹 [1,2,2,3,4,4,3] 是對稱的。 1 / \ 2 2 / \ / \ 3

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 [1,2,2,3,4,4,3

LeetCode 101.Symmetric Tree (對稱二叉樹)

題目描述: 給定一個二叉樹,檢查它是否是映象對稱的。 例如,二叉樹 [1,2,2,3,4,4,3] 是對稱的。 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面這個 [1,2,2,null,3,null,3] 則不是映象對稱的

leetcode 101. Symmetric Tree

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * T

leetcode: 101. Symmetric Tree

Difficulty Easy. Problem # Given a binary tree, check whether it is a mirror of itself (ie, symmetric

學習輪播後的一點心得

關於學習輪播後的一點小心得 這裡直接貼出HTML程式碼,相關解釋會放在程式碼前後。 原理說明(個人理解):將所有圖片放在一個div中的同時,使其左浮動呈現在一行;同時用一個容器包裹住,將容器的大小設定為一張圖片的大小,超出的部分隱藏起來;通過控制圖片的div的left值,向左或向

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

近日使用Taro框架的一點心得

數據 但是 權限 框架 neu 限制 ron 數組 設置 p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 19.0px; font: 13.0px "Helvetica Neue"; color: #000000 }

leetcode 101. Symmetric Tree(C語言,二叉樹,遞迴,判對稱性)30

貼原題: 解析:   本題是給出一個二叉樹,讓判斷其是否左右對稱。   我的思路就是直接遞迴。新建一個遞迴函式,引數是其左右孩子節點,若兩個節點都存在且值相等則對稱,繼續比較其各自的左右孩子。

LeetCode(51) 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

LeetCodeSymmetric Tree 判斷一棵樹是否是映象的

<span style="font-size:18px;"><span style="font-size:18px;">/**LeetCode Symmetric Tree 對稱的樹 * 思路:判斷一棵樹是否對稱,1.有左子樹就要有右子樹 *

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 [1,2,2,3