Construct Binary Tree from Inorder and Postorder Traversal(C++从中序与后序遍历序列构造二叉树)

459 篇文章 1 订阅


(1)递归+二叉树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* buildTree(vector<int>& in, vector<int>& post) {
        if(in.size()==0 && post.size()==0) return nullptr;
        TreeNode *root=new TreeNode(post[post.size()-1]);
        if(in.size()==1 && post.size()==1) {
            root->left=nullptr;
            root->right=nullptr;
            return root;
        }
    
        auto it=find(in.begin(),in.end(),post[post.size()-1]);
        int index=it-in.begin();
        
        vector<int> l_in,l_post,r_in,r_post;
        for(int i=0;i<in.size();i++) {
            if(i<index) l_in.push_back(in[i]);
            else if(i>index) r_in.push_back(in[i]);
        }
        
        for(int i=0;i<post.size()-1;i++) {
            if(i<index) l_post.push_back(post[i]);
            else r_post.push_back(post[i]);
        }
        
        root->left=buildTree(l_in,l_post);
        root->right=buildTree(r_in,r_post);
        
        return root;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉树是一种树形结构,其中每个节点最多有两个子节点。建立二叉树的方法有很多种,其中一种常用的方法是通过输入带空格的前序序列建立二叉链表。具体步骤如下: 1. 读取输入的带空格的前序序列,将其存储在一个数组中。 2. 定义一个栈,将根节点入栈。 3. 从数组中取出下一个元素,如果该元素不为空,则将其作为当前节点的左子节点,并将该节点入栈。 4. 如果该元素为空,则从栈中弹出一个节点,将其作为当前节点,并从数组中取出下一个元素,将其作为当前节点的右子节点,并将该节点入栈。 5. 重复步骤3和4,直到数组中的所有元素都被处理完毕。 建立二叉树后,可以通过递归或非递归的方式进行先序、中序后序遍历。其中,先序遍历的顺序是先遍历根节点,然后遍历左子树,最后遍历右子树;中序遍历的顺序是先遍历左子树,然后遍历根节点,最后遍历右子树;后序遍历的顺序是先遍历左子树,然后遍历右子树,最后遍历根节点。 以下是PHP基于非递归算法实现先序、中序后序遍历二叉树的代码示例: ```php // 定义二叉树节点类 class TreeNode { public $val; public $left; public $right; public function __construct($val) { $this->val = $val; $this->left = null; $this->right = null; } } // 建立二叉树 function buildTree($preorder) { $stack = array(); $root = new TreeNode(array_shift($preorder)); array_push($stack, $root); while (!empty($preorder)) { $node = array_shift($preorder); if ($node !== null) { $cur = new TreeNode($node); $stack[count($stack) - 1]->left = $cur; array_push($stack, $cur); } else { $stack[count($stack) - 1]->right = null; array_pop($stack); } } return $root; } // 非递归实现先序遍历 function preorderTraversal($root) { $stack = array(); $res = array(); array_push($stack, $root); while (!empty($stack)) { $node = array_pop($stack); if ($node !== null) { array_push($res, $node->val); array_push($stack, $node->right); array_push($stack, $node->left); } } return $res; } // 非递归实现中序遍历 function inorderTraversal($root) { $stack = array(); $res = array(); $cur = $root; while ($cur !== null || !empty($stack)) { while ($cur !== null) { array_push($stack, $cur); $cur = $cur->left; } $node = array_pop($stack); array_push($res, $node->val); $cur = $node->right; } return $res; } // 非递归实现后序遍历 function postorderTraversal($root) { $stack = array(); $res = array(); array_push($stack, $root); while (!empty($stack)) { $node = array_pop($stack); if ($node !== null) { array_push($res, $node->val); array_push($stack, $node->left); array_push($stack, $node->right); } } return array_reverse($res); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值