The reason this doesn’t work is that res
only has the value of the first node you give it appended to it; each time you recursively recall the function, it just makes a new res. It is a simple fix though, as follows:
class Solution(object): def inorderTraversal(self, root): res = [] if root: res = self.inorderTraversal(root.left) res.append(root.val) res = res + self.inorderTraversal(root.right) return res
In this, it returns the left branch, the value, and then the right. This can be done much more briefly as follows:
class Solution(object): def inorderTraversal(self, root): return (self.inorderTraversal(root.left) + [root.val] + self.inorderTraversal
Related Posts:
- How to have a set of sets in Python?
- How to obtain a subarray in python 3
- How can I reverse a list in Python?
- Difference between del, remove, and pop on lists
- How can I reverse a list in Python?
- What is the difference between Python’s list methods append and extend?
- Understanding slice notation
- Understanding slice notation
- How to convert list to string [duplicate]
- How do I get the number of elements in a list?
- Difference between del, remove, and pop on lists
- Finding the index of an item in a list
- How to make a flat list out of a list of lists
- Removing duplicates in lists
- Accessing the index in ‘for’ loops?
- List changes unexpectedly after assignment. Why is this and how can I prevent it?
- TypeError: ‘float’ object is not subscriptable
- Is there a short contains function for lists?
- TypeError: ‘float’ object is not subscriptable
- Transpose/Unzip Function (inverse of zip)?
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- Finding the average of a list
- Print a list in reverse order with range()?
- Get unique values from a list in python [duplicate]
- Why is it string.join(list) instead of list.join(string)?
- Why is it string.join(list) instead of list.join(string)?
- How do I concatenate two lists in Python?
- if else in a list comprehension
- Finding median of list in Python
- Python set to list
- Python List vs. Array – when to use?
- How to find all occurrences of an element in a list
- Finding and replacing elements in a list
- How to find all the indexes of a recurring item in a list?
- ValueError: max() arg is an empty sequence
- How do I concatenate two lists in Python?
- Replace values in list using Python
- Writing a list to a file with Python
- Python: Get the first character of the first string in a list?
- Understanding slice notation
- How do I find the duplicates in a list and create another list with them?
- Add list to set?
- What’s the idiomatic syntax for prepending to a short python list?
- How to remove an element from a list by index
- Apply function to each element of a list
- Remove all occurrences of a value from a list?
- How do I append lists in Prolog?
- TypeError: list indices must be integers or slices, not str
- In Python, when to use a Dictionary, List or Set?
- Check if two unordered lists are equal
- ‘list’ object has no attribute ‘shape’
- How do I find the duplicates in a list and create another list with them?
- How can I compare two lists in python and return matches
- How do I check if a list is empty?
- How can I get list of values from dict?
- IndexError: list index out of range and python
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- How to sort a list of objects based on an attribute of the objects?
- Pop index out of range
- AttributeError: ‘list’ object has no attribute ‘replace’ when trying to remove character
- Print list without brackets in a single row
- How do you split a list into evenly sized chunks?
- How to iterate through two lists in parallel?
- How to remove \n from a list element?
- How to return dictionary keys as a list in Python?
- Defining lists as global variables in Python
- Subtracting two lists in Python
- Get the cartesian product of a series of lists?
- Python 2: AttributeError: ‘list’ object has no attribute ‘strip’
- What does the “x for x in” syntax mean?
- How to make a set of lists
- numpy-equivalent of list.pop?
- Create nice column output in python
- TypeError: only integer arrays with one element can be converted to an index 3
- How to create a numpy array of lists?
- Logical indexing with lists
- How does str(list) work?
- convert csv file to list of dictionaries
- Element-wise addition of 2 lists?
- How to split elements of a list?
- Find object in list that has attribute equal to some value (that meets any condition)
- Turn a single number into single digits Python
- List Length in Prolog
- Iterating through list of list in Python
- Python cant convert ‘list’ object to str error [closed]
- Get difference between two lists
- How is Python’s List Implemented?
- Determine if 2 lists have the same elements, regardless of order? [duplicate]
- Building a list inside a list in python
- Sorting list based on values from another list
- Create a list with initial capacity in Python
- Introductory paragraph only in list of blog posts
- List all categories in options
- To get list of bottom most or deepest or last child for specified parent category
- How do I create an alphabetically organized glossary of links?
- adding a #hash to each link in list_pages()?
- Output categories in something different than a list
- get_all_category_ids for bookmark
- Add thumbnails in ‘li’ list
- List child pages alphabetically