二叉树基本操作

1 二叉树的遍历

(1)二叉树的前序遍历

递归实现:

     public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        preOrder(root, res);
        return res;
    }

    public void preOrder(TreeNode root, List<Integer> res) {
        if (root == null)
            return;
        //先打印当前节点,然后打印左子树,最后再打印右子树
        res.add(root.val);
        preOrder(root.left, res);
        preOrder(root.right, res);
    }

利用栈实现

   public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        //加个边界条件判断
        if (root == null)
            return res;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);//压栈
        while (!stack.empty()) {
            TreeNode t1 = stack.pop();//出栈
            res.add(t1.val);
            if (t1.right != null) {
                stack.push(t1.right);
            }
            if (t1.left != null) {
                stack.push(t1.left);
            }
        }
        return res;
    }

(2)二叉树的中序遍历

递归实现

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        inOrderTraversal(root, res);
        return res;
    }

    public void inOrderTraversal(TreeNode root, List<Integer> res) {
        if (root == null)
            return;
        //先打印左子树,然后打印当前节点,最后再打印右子树
        inOrderTraversal(root.left, res);
        res.add(root.val);
        inOrderTraversal(root.right, res);
    }

栈实现

    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        //加个边界条件判断
        if (root == null)
            return res;
        Stack<TreeNode> stack = new Stack<>();
        while (root != null || !stack.isEmpty()) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            if (!stack.isEmpty()) {
                root = stack.pop();
                res.add(root.val);
                root = root.right;
            }
        }
        return res;
    }

(3)二叉树的后序遍历

递归

    void dfs(TreeNode* root, vector<int>& ret){
        if(root == nullptr){
            return;
        }
        dfs(root->left,ret);        // 左
        dfs(root->right,ret);       // 右
        ret.push_back(root->val);   // 根
    }

非递归

vector<int> postorderTraversal(TreeNode* root) {
       TreeNode* p = root;
       vector<int> ret;
       stack<TreeNode*> s1;
       stack<TreeNode*> s2;
       if(p!=nullptr){
           s1.push(p);      // 头结点先压入s1中
       }
       while(!s1.empty()){
           p = s1.top();		// 每次从s1中弹出一个结点
           s1.pop();			// 弹出的结点压入s2中
           s2.push(p);

           if(p->left != nullptr){      // 先压左
               s1.push(p->left);
           }
           if(p->right != nullptr){
               s1.push(p->right);       // 再压右
           }
       }

       // 这样s1的顺序就是 根右左,s2的顺序就是左右根
       while( !s2.empty() ){
           ret.push_back(s2.top()->val);
           s2.pop();
       }
       return ret;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肉丸饭团

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值