zoukankan      html  css  js  c++  java
  • 二叉树的镜像

    采用非递归方式解决:

    /**
    public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
    
        public TreeNode(int val) {
            this.val = val;
    
        }
    
    }
    */
    import java.util.*;
    
    public class Solution {
        List<Integer> list = new ArrayList<>();
        public List<Integer> Mirror(TreeNode root) {
             if (root == null) {
                return null;
            }
            Stack<TreeNode> stack = new Stack<TreeNode>();
            stack.push(root);
            while (!stack.isEmpty()) {
                TreeNode node  = stack.pop();
                list.add(node.val);
                //System.out.println(node.val);
                if (node.left != null || node.right != null) {
                    TreeNode temp = node.left;
                    node.left = node.right;
                    node.right = temp;
                }
                if (node.left != null) {
                    stack.push(node.left);
                }
                 if (node.right != null) {
                    stack.push(node.right);
                }
                
               
                
    
            }
            return list;
    
        }
    }
  • 相关阅读:
    SCUT
    模板
    重链剖分
    树的重心
    SCUT
    SCUT
    SCUT
    SCUT
    SCUT
    SCUT
  • 原文地址:https://www.cnblogs.com/zjf-293916/p/9164363.html
Copyright © 2011-2022 走看看