美文网首页
lint0175. 0480.

lint0175. 0480.

作者: 日光降临 | 来源:发表于2019-02-26 20:50 被阅读0次
    • 反转一棵二叉树
    /**
     * Definition of TreeNode:
     * public class TreeNode {
     *     public int val;
     *     public TreeNode left, right;
     *     public TreeNode(int val) {
     *         this.val = val;
     *         this.left = this.right = null;
     *     }
     * }
     */
    public class Solution {
        /**
         * @param root: a TreeNode, the root of the binary tree
         * @return: nothing
         */
        public void invertBinaryTree(TreeNode root) {
            if(root==null)
                return;
            TreeNode tmp = root.left;
            root.left = root.right;
            root.right = tmp;
            invertBinaryTree(root.left);
            invertBinaryTree(root.right);
        }
    }
    
      1. Binary Tree Paths
    /**
     * Definition of TreeNode:
     * public class TreeNode {
     *     public int val;
     *     public TreeNode left, right;
     *     public TreeNode(int val) {
     *         this.val = val;
     *         this.left = this.right = null;
     *     }
     * }
     */
    
    public class Solution {
        /**
         * @param root: the root of the binary tree
         * @return: all root-to-leaf paths
         */
        public List<String> binaryTreePaths(TreeNode root) {
            List<String> ret = new ArrayList<>();
            if(root==null)
                return ret;
            List<String> lret = binaryTreePaths(root.left);
            List<String> rret = binaryTreePaths(root.right);
            for(String path : lret){
                ret.add(root.val+"->"+path);
            }
            for(String path : rret){
                ret.add(root.val+"->"+path);
            }
            if(ret.size()==0)
                ret.add(""+root.val);
            return ret;
        }
    }
    

    相关文章

      网友评论

          本文标题:lint0175. 0480.

          本文链接:https://www.haomeiwen.com/subject/hnwzyqtx.html