1. 程式人生 > >【LeetCode】樹(共94題)

【LeetCode】樹(共94題)

【94】Binary Tree Inorder Traversal 

【95】Unique Binary Search Trees II 

【96】Unique Binary Search Trees 

【98】Validate Binary Search Tree 

寫個函式檢查一棵樹是不是BST

題解:直接判斷左兒子比根小,右兒子比跟大是會WA的... 舉個會WA的例子。

 

View Code

 【99】Recover Binary Search Tree 

【100】Same Tree

【101】Symmetric Tree 

【102】Binary Tree Level Order Traversal 

【103】Binary Tree Zigzag Level Order Traversal 

【104】Maximum Depth of Binary Tree 

【105】Construct Binary Tree from Preorder and Inorder Traversal 

【106】Construct Binary Tree from Inorder and Postorder Traversal 

給中序遍歷 和 後序遍歷, 重建二叉樹。

沒啥難度,一次ac

View Code

 

【107】Binary Tree Level Order Traversal II

【108】Convert Sorted Array to Binary Search Tree 

給一個增序的vector, 建立一棵高度平衡的BST.

這個題一開始懵了,不會做。。。信不信考到你就掛了orz

思路是二分的思想,從root開始建樹,然後左兒子,右兒子。

View Code

 

【110】Balanced Binary Tree 

【111】Minimum Depth of Binary Tree 

【112】Path Sum 

【113】Path Sum II 

【114】Flatten Binary Tree to Linked List 

把一顆二叉樹拍平成一個連結串列。如圖。判空,莫忘。要取一個節點的子節點之前,要判斷這個節點是否為空。

View Code

 

【116】Populating Next Right Pointers in Each Node 

【117】Populating Next Right Pointers in Each Node II 

【124】Binary Tree Maximum Path Sum 

【129】Sum Root to Leaf Numbers 

【144】Binary Tree Preorder Traversal 

【145】Binary Tree Postorder Traversal 

【156】Binary Tree Upside Down 

【173】Binary Search Tree Iterator 

【199】Binary Tree Right Side View 

【222】Count Complete Tree Nodes 

【226】Invert Binary Tree 

【230】Kth Smallest Element in a BST 

【235】Lowest Common Ancestor of a Binary Search Tree 

【236】Lowest Common Ancestor of a Binary Tree 

【250】Count Univalue Subtrees 

【255】Verify Preorder Sequence in Binary Search Tree 

【257】Binary Tree Paths 

【270】Closest Binary Search Tree Value 

【272】Closest Binary Search Tree Value II 

【285】Inorder Successor in BST 

【297】Serialize and Deserialize Binary Tree 

【298】Binary Tree Longest Consecutive Sequence 

【333】Largest BST Subtree 

【337】House Robber III 

【366】Find Leaves of Binary Tree 

【404】Sum of Left Leaves 

【426】Convert Binary Search Tree to Sorted Doubly Linked List 

【428】Serialize and Deserialize N-ary Tree 

【429】N-ary Tree Level Order Traversal 

【431】Encode N-ary Tree to Binary Tree 

【437】Path Sum III 

【449】Serialize and Deserialize BST 

【450】Delete Node in a BST 

【501】Find Mode in Binary Search Tree 

【508】Most Frequent Subtree Sum 

【513】Find Bottom Left Tree Value 

【515】Find Largest Value in Each Tree Row 

【536】Construct Binary Tree from String 

【538】Convert BST to Greater Tree 

【543】Diameter of Binary Tree 

【545】Boundary of Binary Tree 

【549】Binary Tree Longest Consecutive Sequence II 

【559】Maximum Depth of N-ary Tree 

【563】Binary Tree Tilt 

【572】Subtree of Another Tree 

【582】Kill Process 

【589】N-ary Tree Preorder Traversal 

【590】N-ary Tree Postorder Traversal 

【606】Construct String from Binary Tree 

【617】Merge Two Binary Trees 

【623】Add One Row to Tree 

【637】Average of Levels in Binary Tree 

【652】Find Duplicate Subtrees 

【653】Two Sum IV - Input is a BST 

【654】Maximum Binary Tree 

【655】Print Binary Tree 

【662】Maximum Width of Binary Tree 

【663】Equal Tree Partition 

【666】Path Sum IV 

【669】Trim a Binary Search Tree 

【671】Second Minimum Node In a Binary Tree 

【684】Redundant Connection 

【685】Redundant Connection II 

【687】Longest Univalue Path 

【700】Search in a Binary Search Tree 

【701】Insert into a Binary Search Tree 

【742】Closest Leaf in a Binary Tree 

【814】Binary Tree Pruning 

【834】Sum of Distances in Tree 

【863】All Nodes Distance K in Binary Tree 

【865】Smallest Subtree with all the Deepest Nodes 

【872】Leaf-Similar Trees 

【889】Construct Binary Tree from Preorder and Postorder Traversal 

【894】All Possible Full Binary Trees 

【897】Increasing Order Search Tree