日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

按照前序遍历和中序遍历构建二叉树

發(fā)布時(shí)間:2023/12/4 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 按照前序遍历和中序遍历构建二叉树 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

轉(zhuǎn)載自:http://blog.csdn.net/sbitswc/article/details/26433051

Given preorder and inorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

There is an example. _______7______/ \__10__ ___2/ \ /4 3 _8\ /1 11 The preorder and inorder traversals for the binary tree above is: preorder = {7,10,4,3,1,2,8,11} inorder = {4,10,3,1,7,11,8,2}
The first node in preorder alwasy the root of the tree. We can break the tree like:
1st round:
preorder: ?{7}, {10,4,3,1}, {2,8,11}
inorder: ? ? {4,10,3,1}, {7}, {11, 8,2}

_______7______/ \{4,10,3,1} {11,8,2} Since we alreay find that {7} will be the root, and in "inorder" sert, all the data in the left of {7} will construct the left sub-tree. And the right part will construct a right sub-tree. We can the left and right part agin based on the preorder. 2nd round
left part ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?right part
preorder: {10}, {4}, {3,1} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{2}, {8,11}
inorder: ?{4}, {10}, {3,1} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?{11,8}, {2}


_______7______/ \__10__ ___2/ \ /4 {3,1} {11,8} see that, {10} will be the root of left-sub-tree and {2} will be the root of right-sub-tree.
Same way to split {3,1} and {11,8}, yo will get the complete tree now.

_______7______/ \__10__ ___2/ \ /4 3 _8\ /1 11 So, simulate this process from bottom to top with recursion as following code. c++

[cpp] view plain copy
  • TreeNode?*BuildTreePI(??
  • ????vector<int>?&preorder,??
  • ????vector<int>?&inorder,??
  • ????int?p_s,?int?p_e,??
  • ????int?i_s,?int?i_e){??
  • ????if(p_s?>?p_e)?return?NULL;??
  • ????int?pivot?=?preorder[p_s];??
  • ????int?i?=?i_s;??
  • ????for(;i<i_e;i++){??
  • ????????if(inorder[i]?==?pivot)??
  • ????????????break;??
  • ????}??
  • ????int?length1?=?i-i_s-1;??
  • ????int?length2?=?i_e-i-1;??
  • ????TreeNode*?node?=?new?TreeNode(pivot);??
  • ????node->left?=?BuildTreePI(preorder,inorder,p_s+1,length1+p_s+1,i_s,?i-1);??
  • ????node->right?=?BuildTreePI(preorder,?inorder,?p_e-length2,?p_e,?i+1,?i_e);??
  • ????return?node;??
  • }??
  • TreeNode?*buildTree(vector<int>?&preorder,?vector<int>?&inorder)?{??
  • ????return?BuildTreePI(preorder,inorder,0,preorder.size()-1,0,inorder.size()-1);??
  • }??

  • java

    [java] view plain copy
  • public?TreeNode?buildTree(int[]?preorder,?int[]?inorder)?{??
  • ????????return?buildPI(preorder,?inorder,?0,?preorder.length-1,?0,?inorder.length-1);??
  • ????}??
  • ????public?TreeNode?buildPI(int[]?preorder,?int[]?inorder,?int?p_s,?int?p_e,?int?i_s,?int?i_e){??
  • ????????if(p_s>p_e)??
  • ????????????return?null;??
  • ????????int?pivot?=?preorder[p_s];??
  • ????????int?i?=?i_s;??
  • ????????for(;i<i_e;i++){??
  • ????????????if(inorder[i]==pivot)??
  • ????????????????break;??
  • ????????}??
  • ????????TreeNode?node?=?new?TreeNode(pivot);??
  • ????????int?lenLeft?=?i-i_s;??
  • ????????node.left?=?buildPI(preorder,?inorder,?p_s+1,?p_s+lenLeft,?i_s,?i-1);??
  • ????????node.right?=?buildPI(preorder,?inorder,?p_s+lenLeft+1,?p_e,?i+1,?i_e);??
  • ????????return?node;??
  • ????}?

  • 總結(jié)

    以上是生活随笔為你收集整理的按照前序遍历和中序遍历构建二叉树的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。