LeetCode 101 Symmertic Tree(Python詳解及實現)
【題目】
Given a binary tree, check whether it is amirror 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 44 3
But the following [1,2,2,null,3,null,3] isnot:
1
/\
2 2
\ \
3 3
Note:
Bonus points if you could solve it bothrecursively and iteratively.
判斷給定的二叉樹是否對稱
【思路】
判斷每一層從左到右與從右到左是否相等
【Python實現】
# -*-coding: utf-8 -*- """ Createdon Fri Aug 11 17:06:34 2017 @author:Administrator """ #Definition for a binary tree node. classTreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = None classSolution(object): def isSymmetric(self, root): """ :type root: TreeNode :rtype: bool """ if root: return self.symmetriTree(root.left,root.right) return True def symmetriTree(self, p, q): if p is None and q is None: return True if p and q and p.val == q.val: return self.symmetriTree(p.left,q.right) and self.symmetriTree(p.right, q.left) return False if __name__== '__main__': S = Solution() l1 = TreeNode(4) l2 = TreeNode(2) l3 = TreeNode(6) l4 = TreeNode(1) l5 = TreeNode(5) l6 = TreeNode(3) l7 = TreeNode(7) root = l1 l1.left = l2 l1.right = l3 l2.left = l4 l2.right = l5 l3.left = l6 l3.right = l7
相關推薦
LeetCode 101 Symmertic Tree(Python詳解及實現)
【題目】 Given a binary tree, check whether it is amirror of itself (ie, symmetric around its center). For example, this binary tree[1,2,2,3
LeetCode 98 Validate Binary Search Tree(Python詳解及實現)
【題目】 Given a binary tree, determine if it is avalid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a
LeetCode 97 Interleaving String(Python詳解及實現)
【題目】 Given s1, s2, s3, find whether s3 is formedby the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When
LeetCode 72 Edit Distance(Python詳解及實現)
【題目】 Given two words word1 and word2, find theminimum number of steps required to convert word1 to word2. (each operation iscounted as 1
LeetCode 94 Binary Tree Inorder Traversal(Python詳解及實現)
【題目】 Given a binary tree, return the inordertraversal of its nodes' values. For example: Given binary tree [1,null,2,3], 1 \
大資料學習之路101-redis的持久化詳解及主從複製
接下來我們配置一下主從結構的星型模型: 首先將配置檔案複製3份, 然後修改主節點的配置檔案: 首先關閉RDB: 然後關閉AOF: 修改第二個配置檔案: 先修改埠,他不能和主節點的埠衝突: 為了區分是哪個節點打的日誌,我們還需要
B-tree詳解及實現(C語言)
// // MBTree.c // MBTree // // Created by Wuyixin on 2017/8/4. // Copyright © 2017年 Coding365. All rights reserved. // #include "MBTree.h" static K
B+tree詳解及實現(C語言)
// // BPlusTree.c // BPlusTree // // Created by Wuyixin on 2017/8/4. // Copyright © 2017年 Coding365. All rights reserved. // #include "BPlusTree.h"
python中list詳解及實現
list為python中的常用資料型別,其為python中內建類,繼承自object。接下來全面介紹list的常見方法及自己實現類list功能的類建立list建立空list list1 = [] list2 = list()建立並初始化list l
B-Tree索引詳解及聯合索引使用
B-tree索引(或Balanced Tree),是一種很普遍的資料庫索引結構,oracle預設的索引型別(本文也主要依據oracle來講)。其特點是定位高效、利用率高、自我平衡,特別適用於高基數字段,定位單條或小範圍資料非常高效。理論上,使用B-tree在億條資料與100條資料中定位記錄的花銷相同。資料結構
redis配置文件詳解及實現主從同步切換
redis redis主從 redis配置文件詳解及實現主從同步切換redis復制Redis復制很簡單易用,它通過配置允許slave Redis Servers或者Master Servers的復制品。接下來有幾個關於redis復制的非常重要特性:一個Master可以有多個Slaves。Slaves能
微信和支付寶支付模式詳解及實現二
配置 其余 logs https 朋友 一個 target 多租戶 對比 繼上篇《微信和支付寶支付模式詳解及實現》到現在已經有半年時間了,這期間不少朋友在公號留言支付相關的問題,最近正好也在處理公司支付相關的對接,打算寫這篇來做一個更進一步的介紹,同時根據主要的幾個支付
nfs詳解及實現全網備份
1.統一hosts cat /etc/hosts 172.16.1.5 lb01 172.16.1.6 lb02 172.16.1.7 web02 172.16.1.8 web01 172.16.1.51 db01 172.16.1.31 nfs01
堆排序演算法詳解及實現-----------c語言
堆排序原理: 堆排序指的是將大堆(小堆)堆頂(即下標為0)元素與堆的最後一個(即下標為hp->size - 1)元素交換,hp->size–,將其餘的元素再次調整成大堆(小堆),再次將堆頂(即下標為0)元素與堆的最後一個(即下標為hp->s
二叉搜尋樹詳解及實現程式碼(BST)
概念 二叉搜尋樹(Binary Search Tree),又稱二叉排序樹,它或者是一顆空樹,或者具有如下性質的樹: 若它的左子樹不為空,則左子樹上所有節點的值都小於根節點的值 若它的右子樹不為空,則右子樹上所有節點的值都大於根節點的值 它的左右子樹也分別
CDN技術詳解及實現原理
一本好的入門書是帶你進入陌生領域的明燈,《CDN技術詳解》絕對是帶你進入CDN行業的那盞最亮的明燈。因此,雖然只是純粹的重點抄錄,我也要把《CDN技術詳解》的精華放上網。公諸同好。 第一章 引言 “第一公里”是指全球資訊網流量向用戶傳送的第一個出口,是網站伺服器接入網際網路的鏈路所能提供
Android之viewstub用法詳解及實現延遲載入
上一篇的佈局中間就用了viewstub這個控制元件,現在來說一下其作用和用法" ViewStub 是一個不可見的,大小為0的View,最佳用途就是實現View的延遲載入,避免資源浪費,在需要的時候才載入View"需要注意的是,載入view之後,viewstub本身就會被新載入
C++11--智慧指標詳解及實現
1、基礎概念 智慧指標的一種通用實現技術是使用引用計數。智慧指標類將一個計數器與智慧指標指向的物件相關聯,用來記錄有多少個智慧指標指向相同的物件,並在恰當的時候釋放物件。 每次建立類的新物件時,初始化指標並將引用計數置為1;當物件作為另一物件的副本而建立時,引用計數加1;對一個物件進行賦
揹包問題——“完全揹包”詳解及實現(包含揹包具體物品的求解)
原文地址:http://blog.csdn.net/wumuzi520/article/details/7014830 完全揹包是在N種物品中選取若干件(同一種物品可多次選取)放在空間為V的揹包裡,每種物品的體積為C1,C2,…,Cn,與之相對應的價值為W1
最大流之Ford-Fulkerson方法詳解及實現
最大流問題常常出現在物流配送中,可以規約為以下的圖問題。最大流問題中,圖中兩個頂點之間不能同時存在一對相反方向的邊。 邊上的數字為該條邊的容量,即在該條邊上流過的量的上限值。最大流問題就是在滿足容量限制條件下,使從起點s到終點t的流量達到最大。在介紹解決最大流問題的For