遞迴 面試題 已知兩個遍歷 建立樹

2021-10-24 21:40:29 字數 3443 閱讀 7081

例如已知先序遍歷求中序遍歷;

前序遍歷 preorder = [3,9,20,15,7]

中序遍歷 inorder = [9,3,15,20,7]

思路其實挺簡單的,每次在pre中找根結點,在inorder找左節點與右節點

#include

#include

using

namespace std;

struct treenode };

class

solution

treenode *root =

newtreenode

(preorder[preleft]);

int index;

for(index = inleft;index <= inright;index++

)//找到了先序在中序中的位置

} root-

>left=

_buildtree

(preorder, preleft+

1, preleft+ index -inleft, inorder, inleft, index-1)

; root-

>right =

_buildtree

(preorder, preleft + index - inleft +

1, preright, inorder, index +

1, inright)

;return root;

}public

: treenode*

buildtree

(vector<

int>

& preorder, vector<

int>

& inorder)

};

當然每次根據pre在in中找位置可能太浪費時間,我們採用空間換時間,採用unorderrd_map訪問中序的位置

class

solution

treenode *root =

newtreenode

(preorder[preleft]);

int index= index_map[preorder[preleft]];

root-

>left=

_buildtree

(preorder, preleft+

1, preleft+ index -inleft, index_map, inleft, index-1)

; root-

>right =

_buildtree

(preorder, preleft + index - inleft +

1, preright, index_map, index +

1, inright)

;return root;

}public

: treenode*

buildtree

(vector<

int>

& preorder, vector<

int>

& inorder)

return

_buildtree

(preorder, preleft, preright, index_map, inleft, inright);}

};

當然還有已知後序和中序,建立二叉樹。

思路一樣首先在後序中倒數第乙個是根,然後在中序中找左右子節點,遞迴

**如下:

/**

* definition for a binary tree node.

* struct treenode

* };

*/class

solution

treenode *root =

newtreenode

(postorder[posright]);

int index;

for(index=inleft;index<=inright;index++

)//由於是後序遍歷,所以是左右根

} root-

>left=

_buildtree

(postorder, posleft, posright+index-inright-

1, inorder, inleft, index-1)

; root-

>right =

_buildtree

(postorder, posright + index - inright, posright -

1, inorder, index +

1,inright)

;return root;//}

public

: treenode*

buildtree

(vector<

int>

& inorder, vector<

int>

& postorder)

};

當然也可以進行優化採用undered_map儲存中序的數值和下標的關係

#include

#include

#include

using

namespace std;

struct treenode };

class

solution

treenode *root =

newtreenode

(postorder[posright]);

int index= index_map[postorder[posright]];

root-

>left=

_buildtree

(postorder, posleft, posright+index-inright-

1, index_map, inleft, index-1)

; root-

>right =

_buildtree

(postorder, posright + index - inright, posright -

1, index_map, index +

1,inright)

;return root;//}

public

: treenode*

buildtree

(vector<

int>

& inorder, vector<

int>

& postorder)

return

_buildtree

(postorder, posleft, posright, index_map, inleft, inright);}

};

兩個面試題

1.實現乙個lite版的字串替換函式 c c char strreplace char str,const char sub,const char rep 限制條件和要求如下 1.其中str為原字串,sub為待被替換的子串。為簡單起見,假定字串sub和rep長度一樣 2.直接對原字串str進行修改並...

兩個簡單的面試題

分享兩個簡單的面試題,好吧,求質數的那個面試題居然沒做出來。要好好打基礎 public class study0812 獲取arr陣列中最大的數的索引,該索引加97就是字元中重複最多的字元 int maxindex 0 for int i 0 i arr.length 1 i return char...

面試題 兩個佇列實現棧

兩個佇列實現棧 前提已知 typedef struct queue queue void initqueue queue q void enqueue queue q,int key int dequeue queue q int sizeofqueue queue q int isqueueemp...