mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge pull request #397 from X-shuffle/master
添加 0106.从中序与后序遍历序列构造二叉树 go版本
This commit is contained in:
@ -693,6 +693,70 @@ class Solution:
|
||||
return root
|
||||
```
|
||||
Go:
|
||||
> 106 从中序与后序遍历序列构造二叉树
|
||||
|
||||
```go
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
func buildTree(inorder []int, postorder []int) *TreeNode {
|
||||
if len(inorder)<1||len(postorder)<1{return nil}
|
||||
//先找到根节点(后续遍历的最后一个就是根节点)
|
||||
nodeValue:=postorder[len(postorder)-1]
|
||||
//从中序遍历中找到一分为二的点,左边为左子树,右边为右子树
|
||||
left:=findRootIndex(inorder,nodeValue)
|
||||
//构造root
|
||||
root:=&TreeNode{Val: nodeValue,
|
||||
Left: buildTree(inorder[:left],postorder[:left]),//将后续遍历一分为二,左边为左子树,右边为右子树
|
||||
Right: buildTree(inorder[left+1:],postorder[left:len(postorder)-1])}
|
||||
return root
|
||||
}
|
||||
func findRootIndex(inorder []int,target int) (index int){
|
||||
for i:=0;i<len(inorder);i++{
|
||||
if target==inorder[i]{
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
```
|
||||
|
||||
> 105 从前序与中序遍历序列构造二叉树
|
||||
|
||||
```go
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
func buildTree(preorder []int, inorder []int) *TreeNode {
|
||||
if len(preorder)<1||len(inorder)<1{return nil}
|
||||
left:=findRootIndex(preorder[0],inorder)
|
||||
root:=&TreeNode{
|
||||
Val: preorder[0],
|
||||
Left: buildTree(preorder[1:left+1],inorder[:left]),
|
||||
Right: buildTree(preorder[left+1:],inorder[left+1:])}
|
||||
return root
|
||||
}
|
||||
func findRootIndex(target int,inorder []int) int{
|
||||
for i:=0;i<len(inorder);i++{
|
||||
if target==inorder[i]{
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
JavaScript
|
||||
|
@ -224,8 +224,37 @@ class Solution:
|
||||
return r
|
||||
```
|
||||
Go:
|
||||
> 中序遍历,然后计算最小差值
|
||||
|
||||
|
||||
```go
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
func getMinimumDifference(root *TreeNode) int {
|
||||
var res []int
|
||||
findMIn(root,&res)
|
||||
min:=1000000//一个比较大的值
|
||||
for i:=1;i<len(res);i++{
|
||||
tempValue:=res[i]-res[i-1]
|
||||
if tempValue<min{
|
||||
min=tempValue
|
||||
}
|
||||
}
|
||||
return min
|
||||
}
|
||||
//中序遍历
|
||||
func findMIn(root *TreeNode,res *[]int){
|
||||
if root==nil{return}
|
||||
findMIn(root.Left,res)
|
||||
*res=append(*res,root.Val)
|
||||
findMIn(root.Right,res)
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
-----------------------
|
||||
|
@ -332,6 +332,43 @@ class Solution:
|
||||
|
||||
Go:
|
||||
|
||||
```go
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
//前序遍历(递归遍历,跟105 106差不多的思路)
|
||||
func mergeTrees(t1 *TreeNode, t2 *TreeNode) *TreeNode {
|
||||
var value int
|
||||
var nullNode *TreeNode//空node,便于遍历
|
||||
nullNode=&TreeNode{
|
||||
Val:0,
|
||||
Left:nil,
|
||||
Right:nil}
|
||||
switch {
|
||||
case t1==nil&&t2==nil: return nil//终止条件
|
||||
default : //如果其中一个节点为空,则将该节点置为nullNode,方便下次遍历
|
||||
if t1==nil{
|
||||
value=t2.Val
|
||||
t1=nullNode
|
||||
}else if t2==nil{
|
||||
value=t1.Val
|
||||
t2=nullNode
|
||||
}else {
|
||||
value=t1.Val+t2.Val
|
||||
}
|
||||
}
|
||||
root:=&TreeNode{//构造新的二叉树
|
||||
Val: value,
|
||||
Left: mergeTrees(t1.Left,t2.Left),
|
||||
Right: mergeTrees(t1.Right,t2.Right)}
|
||||
return root
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
@ -278,6 +278,39 @@ class Solution:
|
||||
|
||||
Go:
|
||||
|
||||
> 654. 最大二叉树
|
||||
|
||||
```go
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
func constructMaximumBinaryTree(nums []int) *TreeNode {
|
||||
if len(nums)<1{return nil}
|
||||
//首选找到最大值
|
||||
index:=findMax(nums)
|
||||
//其次构造二叉树
|
||||
root:=&TreeNode{
|
||||
Val: nums[index],
|
||||
Left:constructMaximumBinaryTree(nums[:index]),//左半边
|
||||
Right:constructMaximumBinaryTree(nums[index+1:]),//右半边
|
||||
}
|
||||
return root
|
||||
}
|
||||
func findMax(nums []int) (index int){
|
||||
for i:=0;i<len(nums);i++{
|
||||
if nums[i]>nums[index]{
|
||||
index=i
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -241,6 +241,56 @@ class Solution:
|
||||
|
||||
Go:
|
||||
|
||||
> 递归法
|
||||
|
||||
```go
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
//递归法
|
||||
func searchBST(root *TreeNode, val int) *TreeNode {
|
||||
if root==nil||root.Val==val{
|
||||
return root
|
||||
}
|
||||
if root.Val>val{
|
||||
return searchBST(root.Left,val)
|
||||
}
|
||||
return searchBST(root.Right,val)
|
||||
}
|
||||
```
|
||||
|
||||
> 迭代法
|
||||
|
||||
```go
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
//迭代法
|
||||
func searchBST(root *TreeNode, val int) *TreeNode {
|
||||
for root!=nil{
|
||||
if root.Val>val{
|
||||
root=root.Left
|
||||
}else if root.Val<val{
|
||||
root=root.Right
|
||||
}else{
|
||||
break
|
||||
}
|
||||
}
|
||||
return root
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -214,6 +214,52 @@ Python:
|
||||
|
||||
Go:
|
||||
|
||||
> 暴力法
|
||||
|
||||
```go
|
||||
func dailyTemperatures(temperatures []int) []int {
|
||||
length:=len(temperatures)
|
||||
res:=make([]int,length)
|
||||
for i:=0;i<length;i++{
|
||||
j:=i+1
|
||||
if i==length-1{
|
||||
res[i]=0
|
||||
}
|
||||
for j<length&&temperatures[i]>=temperatures[j]{//大于等于
|
||||
j++
|
||||
}
|
||||
if j<length&&temperatures[i]<temperatures[j]{
|
||||
res[i]=j-i
|
||||
}
|
||||
if j==length{
|
||||
res[i]=0
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
> 单调栈法
|
||||
|
||||
```go
|
||||
func dailyTemperatures(temperatures []int) []int {
|
||||
length:=len(temperatures)
|
||||
res:=make([]int,length)
|
||||
stack:=[]int{}
|
||||
for i:=0;i<length;i++{
|
||||
//如果当前栈中存在元素,新来的元素大于栈顶的元素,则计算差值并弹出栈顶元素
|
||||
for len(stack)>0&&temperatures[i]>temperatures[stack[len(stack)-1]]{
|
||||
res[stack[len(stack)-1]]=i-stack[len(stack)-1]//存放结果集
|
||||
stack=stack[:len(stack)-1]//删除stack[len(stack)-1]的元素
|
||||
}
|
||||
//如果栈顶元素大于等于新来的元素,则加入到栈中。当栈内元素个数为0时,直接入栈
|
||||
if len(stack)==0||temperatures[i]<=temperatures[stack[len(stack)-1]]{
|
||||
stack = append(stack, i)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user