PAT 1138 Postorder Traversal(二叉樹的儲存和遍歷)
題意:給出二叉樹的前序和中序遍歷,給出其後序遍歷的第一個元素。
思路:根據前序和中序遍歷的結果得到二叉樹的具體構造,再進行後序遍歷。
程式碼:
#include <cstdio> #include <cstring> #include <iostream> using namespace std; int pre[50010],in[50010]; int lSon[50010],rSon[50010];//按照前序遍歷存 int BuildTree(int l1,int l2,int num){//前序和中序遍歷對於兩個陣列的起始位置;當前子樹的元素數量 if(num <= 0) return -1; int ro,index; ro = l1; index = l2; while(in[index++] != pre[ro]); lSon[ro] = BuildTree(l1 + 1,l2,index - l2 - 1); rSon[ro] = BuildTree(l1 + index - l2,index,num - index + l2); return ro; } int PostOrder(int root){ if(lSon[root] != -1){ return PostOrder(lSon[root]); } else if(rSon[root] != -1){ return PostOrder(rSon[root]); } else return root; } int main(){ int n,root,re; scanf("%d",&n); for(int i = 0;i < n;i++){ scanf("%d",&pre[i]); } for(int i = 0;i < n;i++){ scanf("%d",&in[i]); } root = BuildTree(0,0,n); re = PostOrder(root); printf("%d\n",pre[re]); return 0; }
相關推薦
LeetCode 145 Binary Tree Postorder Traversal(二叉樹的興許遍歷)+(二叉樹、叠代)
int truct fin for data- right class span popu 翻譯 給定一個二叉樹。返回其興許遍歷的節點的值。 比如: 給定二叉樹為 {1。 #, 2, 3} 1 2 / 3 返回
PAT 1138 Postorder Traversal(二叉樹的儲存和遍歷)
題意:給出二叉樹的前序和中序遍歷,給出其後序遍歷的第一個元素。 思路:根據前序和中序遍歷的結果得到二叉樹的具體構造,再進行後序遍歷。 程式碼: #include <cstdio> #in
[LintCode] Binary Tree Level Order Traversal(二叉樹的層次遍歷)
描述 給出一棵二叉樹,返回其節點值的層次遍歷(逐層從左往右訪問) 樣例 給一棵二叉樹 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分層遍歷結果:
LeetCode:102. Binary Tree Level Order Traversal(二叉樹的層次遍歷)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given b
LeetCode | Binary Tree Level Order Traversal(二叉樹層序遍歷)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Gi
LeetCode 144 Binary Tree Preorder Traversal(二叉樹前序遍歷)
Given a binary tree, return thepreordertraversal of its nodes' values. For example: Given binary tr
Binary Tree Level Order Traversal(二叉樹層序遍歷-儲存並返回結果集)
題目描述 Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). For example: Given
Leetcode 103 二叉樹的鋸齒形層次遍歷 (二叉樹的層次遍歷)
給定一個二叉樹,返回其節點值的鋸齒形層次遍歷。(即先從左往右,再從右往左進行下一層遍歷,以此類推,層與層之間交替進行)。 例如: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回鋸
Leetcode 103 二叉樹的鋸齒形層次遍歷 (二叉樹的層次遍歷)
給定一個二叉樹,返回其節點值的鋸齒形層次遍歷。(即先從左往右,再從右往左進行下一層遍歷,以此類推,層與層之間交替進行)。 例如: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7
劍指Offer——樹:把二叉樹列印成多行(二叉樹層序遍歷)
對於二叉樹的最好的解決辦法就是遞迴。遍歷方法無外乎先序遍歷,中序遍歷,後序遍歷方法以及層序遍歷方法。這裡給大家安利一個關於樹的面試題的連結,博主walkinginthewind比較全面且詳細的介紹了二叉樹相關的面試題:對於層序遍歷,最好的方法就是用佇列記錄遍歷節點的值,按層列
LeetCode 199 Binary Tree Right Side View(二叉樹層序遍歷)
Given a binary tree, imagine yourself standing on therightside of it, return the values of the node
Tree UVA - 548(二叉樹遞歸遍歷)
out pac col sstream end 遍歷 std 二叉 con 題目鏈接:https://vjudge.net/problem/UVA-548 題目大意:給一顆點帶權(權值各不相同,都是小於10000的正整數)的二叉樹的中序遍歷和後序遍歷,找一個葉子結點使得它
【LeetCode-面試演算法經典-Java實現】【145-Binary Tree Postorder Traversal(二叉樹非遞迴後序遍歷)】
原題 Given a binary tree, return the postorder traversal of its nodes’ values. For exampl
【leetcode】145Binary Tree Postorder Traversal(二叉樹非遞迴後序遍歷)
二叉樹後序遍歷非遞迴方法很多書和部落格已經講的很清楚啦,這裡就是記錄一下方便自己日後看 基本思路是: 利用棧來實現 先找到最左節點,過程中的節點都入棧 如果該節點沒有右孩子或前一步訪問了右孩子(根據後序遍歷二叉樹的特點可以知道,如果當前節點有右孩子,則訪問當前節點前一定是
【LeetCode-面試算法經典-Java實現】【107-Binary Tree Level Order Traversal II(二叉樹層序遍歷II)】
lin -m length ret itl pub util 實現類 markdown 【107-Binary Tree Level Order Traversal II(二叉樹層序遍歷II)】 【LeetCode-面試算法經典-Java實現】【全
LeetCode | Binary Tree Level Order Traversal II(二叉樹層序遍歷II)
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf
LeetCode 107.Binary Tree Level Order Traversal II (二叉樹的層次遍歷 II)
題目描述: 給定一個二叉樹,返回其節點值自底向上的層次遍歷。 (即按從葉子節點所在層到根節點所在的層,逐層從左向右遍歷) 例如: 給定二叉樹 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7
[LeetCode] Binary Tree Level Order Traversal II 二叉樹層序遍歷之二
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For ex
LeetCode 102 Binary Tree Level Order Traversal(二叉樹的層次遍歷)
一,問題描述: 1,給定一棵二叉樹,返回它的層次遍歷。 2,例如: 3,解題思路: 使用LinkedList queue=new LinkedList()。因為LinkedList具有佇列的性質,即先進先出規則。然後統計每一層的節點個數,然後
【LeetCode筆記】Binary Tree Level Order Traversal II 二叉樹按層遍歷,反向輸出
題目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from