From d85a14521f45cc2a38875fe942548f968fb7af94 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Fri, 2 Dec 2022 00:53:19 +0800 Subject: [PATCH] Fix the util of array to tree. --- README.md | 2 +- codes/java/include/TreeNode.java | 3 +++ codes/python/include/binary_tree.py | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 24391cfb9..a006289c5 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ ## 关于本书 -本书致力于达成以下目标: +本书面向数据结构与算法初学者,致力于达成以下目标: - 开源免费,所有同学都可在网上获取本书; - 新手友好,适合算法初学者自主学习入门; diff --git a/codes/java/include/TreeNode.java b/codes/java/include/TreeNode.java index bba76b123..6541064f7 100755 --- a/codes/java/include/TreeNode.java +++ b/codes/java/include/TreeNode.java @@ -26,6 +26,9 @@ public class TreeNode { * @return */ public static TreeNode arrToTree(Integer[] arr) { + if (arr.length == 0) + return null; + TreeNode root = new TreeNode(arr[0]); Queue queue = new LinkedList<>() {{ add(root); }}; int i = 1; diff --git a/codes/python/include/binary_tree.py b/codes/python/include/binary_tree.py index d905d8270..24acb47d4 100644 --- a/codes/python/include/binary_tree.py +++ b/codes/python/include/binary_tree.py @@ -24,7 +24,7 @@ def list_to_tree(arr): [type]: [description] """ if not arr: - return + return None i = 1 root = TreeNode(int(arr[0])) queue = collections.deque()